<?php

class Usuario_model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Monta grid do usuário
     * @param  [type] $sort_field [description]
     * @param  [type] $order      [description]
     * @param  [type] $start      [description]
     * @param  [type] $limite     [description]
     * @param  [type] $filtro     [description]
     * @return [type]             [description]
     */
    public function get_to_grid($sort_field, $order, $start, $limite, $filtro)
    {

        $this->db->select("u.us_id, u.us_nome, u.us_status, u.us_login, u.us_agente, pr_nome", FALSE);
        $this->db->join("perfil p", "u.pr_id = p.pr_id", "JOIN");
        $this->db->from("usuario u");

        // Filtros
        if ($filtro['nome_usuario'] != '')
        {
            $this->db->where("u.us_nome LIKE '%".$filtro['nome_usuario']."%'");
        }
        if ($filtro['check_exibir_inativos'] == '')
        {
            $this->db->where("u.us_status = 1");
        }
        // =======

        $total_registros = $this->db->count_all_results();

        $this->db->order_by("$sort_field $order");
        $this->db->limit($limite, $start);

        $result = $this->db->get()->result();

        $arr['total']  = $total_registros;
        $arr['result'] = $result;

        return $arr;
    }

    public function change_status($new_status, $id_usuario)
    {
        $data = array(
            'us_status' => $new_status

        );

        $this->db->where_in("us_id", $id_usuario);
        $this->db->update('usuario', $data);

        return $this->db->affected_rows();
    }
}